//
//  SelectCourseViewController.swift
//  约健
//
//  Created by tyh on 2017/10/10.
//  Copyright © 2017年 tyh. All rights reserved.
//

import UIKit

class SelectCourseViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    @IBAction func Changed(_ sender: UISegmentedControl) {
        
        switch SegmentedControl.selectedSegmentIndex {
        case 0:
            GetCourseing()
        case 1:
            GetCourse()
        default:
            break;
        }
        

    }

    @IBOutlet weak var SegmentedControl: UISegmentedControl!
    @IBOutlet weak var CourseTable: UITableView!
    var courseingsList:NSArray!
    var coursesList:NSArray!
    var beiyongList:NSArray!
    override func viewDidLoad() {
        super.viewDidLoad()
        //self.view.backgroundColor = UIColor.black
        //self.CourseTable.backgroundColor = UIColor.black
        self.title = "课程列表"
        GetCourseing()
        self.CourseTable.delegate = self
        self.CourseTable.dataSource = self
    

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    
     func numberOfSections(in tableView: UITableView) -> Int {
        if SegmentedControl.selectedSegmentIndex == 0{
        return 1
        }
        else if SegmentedControl.selectedSegmentIndex == 1{
            return 2
        }else{
            return 0
        }
    }
    
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if SegmentedControl.selectedSegmentIndex == 0{
            if courseingsList != nil{
                return courseingsList.count
            }
            return 0
        }
        else if SegmentedControl.selectedSegmentIndex == 1{
            if section == 0{
                return 1
            }else{
            if coursesList != nil{
                return coursesList.count
            }
            return 0
            }
        }else {
            return 0
        }
        
    }
    
    
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var basecell = UITableViewCell()
        if SegmentedControl.selectedSegmentIndex == 0{
            let model:CourseModel = courseingsList[indexPath.row] as! CourseModel
            let cell:CourseTableViewCell = tableView.dequeueReusableCell(withIdentifier: "courseCell", for: indexPath) as! CourseTableViewCell
            cell.Name.text = model.train_name! as String
            let Stri = "点击量:" + "\(model.hits!)"
            cell.Hot.text = Stri
            cell.TypeName.text = model.body_name! as String
            print(model.id)
            
            let imageStr = "http://172.24.10.175/workout/Uploads/" + (model.thumb! as String)
            let imageUrl = URL(string: imageStr)
            let data = try! Data(contentsOf: imageUrl!)
            let image = UIImage(data: data)
            cell.Pic.image = image
            basecell = cell
            
            
        }else if SegmentedControl.selectedSegmentIndex == 1{
        
            
            if indexPath.section == 0{
            
                let cell:ButtonTableViewCell = tableView.dequeueReusableCell(withIdentifier: "buttonCell", for: indexPath) as! ButtonTableViewCell
                cell.allBtn.tag = 0
                cell.allBtn.addTarget(self, action: #selector(display), for: .touchUpInside)
                cell.cainiaoBtn.tag = 2
                cell.cainiaoBtn.addTarget(self, action: #selector(display), for: .touchUpInside)
                cell.rumenBtn.tag = 1
                cell.rumenBtn.addTarget(self, action: #selector(display), for: .touchUpInside)
                cell.jingtongBtn.tag = 3
                cell.jingtongBtn.addTarget(self, action: #selector(display), for: .touchUpInside)
            basecell = cell
        }else{
                
                
                let model:CourseingModel = coursesList[indexPath.row] as! CourseingModel
                let cell:CourseTableViewCell = tableView.dequeueReusableCell(withIdentifier: "courseCell", for: indexPath) as! CourseTableViewCell
                cell.Name.text = model.name! as String
                let Stri = "点击量:" + "\(model.hits!)"
                cell.Hot.text = Stri
                var type:String!
                if model.degree == "1"{
                    type = "入门"
                }else if model.degree == "2"{
                    type = "菜鸟"
                }else if model.degree == "3"{
                    type = "进阶"
                }
                cell.TypeName.text = type
                print(model.id)
                
                let imageStr = "http://172.24.10.175/workout/Uploads/" + (model.thumb! as String)
                let imageUrl = URL(string: imageStr)
                let data = try! Data(contentsOf: imageUrl!)
                let image = UIImage(data: data)
                cell.Pic.image = image
     
            basecell = cell
        }
        }
        
        
        basecell.selectionStyle = UITableViewCellSelectionStyle.none
        return basecell
    }
    
     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if SegmentedControl.selectedSegmentIndex == 0{
            return height * 0.2
        }
        else if SegmentedControl.selectedSegmentIndex == 1{
        if indexPath.section == 0{
            return height * 0.05
        }
        else{
            return height * 0.2
        }
        }
        return 0
    }
    /*
     func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        
        if section == 0 {
            
            return 10
            
        }else{
            
            return 0
        }
    }*/
    
    //获取线上课程列表
    func  GetCourseing(){
        NetWorkMngTool.sharedNetworkMngtool().Network_GetCourseingListView(self.view) { (courseList:NSArray) in
            
            self.courseingsList = courseList
            self.CourseTable.reloadData()
            
        }
    }
    //获取线下课程列表
    func  GetCourse(){
        NetWorkMngTool.sharedNetworkMngtool().Network_GetCourseListView(self.view) { (courseList:NSArray) in
            
            self.coursesList = courseList
            self.beiyongList = courseList
            self.CourseTable.reloadData()
            
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        if SegmentedControl.selectedSegmentIndex == 0{
            let currentcourse:CourseModel = courseingsList[(indexPath as NSIndexPath).row] as! CourseModel
            self.performSegue(withIdentifier: "tocourseingdetail", sender: currentcourse)
        }
            else if SegmentedControl.selectedSegmentIndex == 1{
                let currentcourse:CourseingModel = coursesList[(indexPath as NSIndexPath).row] as! CourseingModel
                self.performSegue(withIdentifier: "tocoursedetail", sender: currentcourse)
            }
        }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "tocourseingdetail"{
            let CourseDetailController:CourseingDetailTableViewController = segue.destination as! CourseingDetailTableViewController
            CourseDetailController.Course = sender as! CourseModel
            
        }else if segue.identifier == "tocoursedetail"{
            let CourseDetailController:CourseDetailTableViewController = segue.destination as! CourseDetailTableViewController
            CourseDetailController.Course = sender as! CourseingModel
            
            
        }
    }
    
    
    
    func display(sender:UIButton){
  
        var object:CourseingModel!
        if sender.tag == 0{
            GetCourse()
        }else if sender.tag == 1{
            let selectList:NSMutableArray = NSMutableArray()
            for i in 0 ..< beiyongList.count {
              object = beiyongList[i] as! CourseingModel
                if object.degree == "1"{
                    selectList.add(object)
                }
                self.coursesList = selectList
                self.CourseTable.reloadData()

            }
        }else if sender.tag == 2{
            let selectList:NSMutableArray = NSMutableArray()
            for i in 0 ..< beiyongList.count {
                object = beiyongList[i] as! CourseingModel
                if object.degree == "2"{
                    selectList.add(object)
                }
                self.coursesList = selectList
                self.CourseTable.reloadData()
        }
        }
            else if sender.tag == 3{
            let selectList:NSMutableArray = NSMutableArray()
            for i in 0 ..< beiyongList.count {
                object = beiyongList[i] as! CourseingModel
                if object.degree == "3"{
                    selectList.add(object)
                }
                self.coursesList = selectList
                self.CourseTable.reloadData()
        }
        }
        
    }
        

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
